﻿#region 程序集 VanillaPsycastsExpanded, Version=1.1.0.0, Culture=neutral, PublicKeyToken=null
// C:\code\MyMod\MOD\bin\this!!!!!!!1\1.4\Assemblies\VanillaPsycastsExpanded.dll
// Decompiled with ICSharpCode.Decompiler 7.1.0.6543
#endregion

using System.Collections.Generic;
using RI_Talisman;
using RimWorld;
using UnityEngine;
using Verse;

namespace WhoXiuXian
{
    public class RI_IncidentWorker_DropBlackSteelChuck : IncidentWorker
    {
        protected override bool CanFireNowSub(IncidentParms parms)
        {
            Map map = (Map)parms.target;
            IntVec3 cell;
            return TryFindCell(out cell, map);
        }

        protected override bool TryExecuteWorker(IncidentParms parms)
        {
            Map map = (Map)parms.target;
            if (!TryFindCell(out var cell, map))
            {
                return false;
            }

            List<Thing> list = new List<Thing>();
            Thing rsc = ThingMaker.MakeThing(RI_T_DefOfs.RI_Building_SpaceShipChunk);
            list.Add(rsc);

            SkyfallerMaker.SpawnSkyfaller(ThingDefOf.MeteoriteIncoming, list, cell, map);
            LetterDef baseLetterDef = (LetterDefOf.PositiveEvent);
            string text = string.Format(def.letterText, list[0].def.label).CapitalizeFirst();
            SendStandardLetter(def.letterLabel, text, baseLetterDef, parms, new TargetInfo(cell, map));
            return true;
        }

        private bool TryFindCell(out IntVec3 cell, Map map)
        {
            int maxMineables = 5;
            return CellFinderLoose.TryFindSkyfallerCell(ThingDefOf.MeteoriteIncoming, map, out cell, 10, default(IntVec3), -1, allowRoofedCells: true, allowCellsWithItems: false, allowCellsWithBuildings: false, colonyReachable: false, avoidColonistsIfExplosive: true, alwaysAvoidColonists: true, delegate (IntVec3 x)
            {
                int num = Mathf.CeilToInt(Mathf.Sqrt(maxMineables)) + 2;
                CellRect cellRect = CellRect.CenteredOn(x, num, num);
                int num2 = 0;
                foreach (IntVec3 item in cellRect)
                {
                    if (item.InBounds(map) && item.Standable(map))
                    {
                        num2++;
                    }
                }

                return num2 >= maxMineables;
            });
        }
    }
}
